﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseWebAPP.code.DAL;
using WarehouseWebAPP.code.DAL.DeliveredDSTableAdapters;

namespace WarehouseWebAPP.code.BLL
{
    public class DeliveredBLL
    {
        private deliveredTableAdapter DeliveredAdapter = null;
        public deliveredTableAdapter Adapter
        {
            get
            {
                if (DeliveredAdapter == null) DeliveredAdapter = new deliveredTableAdapter();
                return DeliveredAdapter;
            }
        }

        public void DeliveredInsert(string delivered_component, string delivered_user, string delivered_customer, DateTime delivered_date, int delivered_quantity)
        {
            if (delivered_component==null)
                throw new Exception ("Delivered Component is missing");
            if (delivered_user== null)
                throw new Exception ("Delivering User is missing");
            if (delivered_customer==null)
                throw new Exception ("Delivered Customer is missing");
            if (delivered_date==null)
                throw new Exception ("Delvering Date is missing");

            //update quantity number
            ComponentBLL CBLL = new ComponentBLL();
            int QuantityBefore=CBLL.ComponentGetQuantityByNum(new Guid(delivered_component));
            CBLL.ComponentQuantityUpdate(new Guid(delivered_component), QuantityBefore-delivered_quantity);
           
            if (Adapter.DeliveredInsert(new Guid (delivered_component),new Guid(delivered_user), new Guid(delivered_customer),delivered_date,delivered_quantity) <1 )
                throw new Exception ("Delivery was not Submitted");

            //log event
            ComponentBLL componenetBLL = new ComponentBLL();
            UserBLL userBLL = new UserBLL();
            CustomerBLL customerBLL = new CustomerBLL();
            string component = componenetBLL.ComponentGetByNum(new Guid(delivered_component))[0].component_serial_num;
            string customer = customerBLL.CustomerGetByNum(new Guid(delivered_customer))[0].customer_name;
            string user = userBLL.userGetByNum(new Guid(delivered_user))[0].user_name;
            string[] Parameters = {component,customer,user,delivered_quantity.ToString()};
            MyEvent ev = History.EventDecorator(History.ActionType.NewDelivery, Parameters, DateTime.Now);
            History.AddEvent(ev);
        }

        public void DeliveredDelete(Guid original_delivered_num)
        {
            if (original_delivered_num == null)
                throw new Exception("Delivey Number is missing");
            if (Adapter.DeliveredDelete(original_delivered_num) < 1)
                throw new Exception("Delivery was not deleted");
        }

        public void DeliveredUpdate(string delivered_component,Guid delivered_user, Guid delivered_customer, DateTime delivered_date, int delivered_quantity, Guid original_delivered_num)
        {
            if (original_delivered_num == null)
                throw new Exception("Delivery Num is missing");
            if (delivered_component == null)
                throw new Exception("Delivered Component is missing");
            if (delivered_user == null)
                throw new Exception("Delivering User is missing");
            if (delivered_customer == null)
                throw new Exception("Delivered Customer is missing");
            if (delivered_date == null)
                throw new Exception("Delvering Date is missing");
            if (Adapter.DeliveredUpdate(new Guid(delivered_component), delivered_user,delivered_customer, delivered_date, delivered_quantity,original_delivered_num) < 1)
                throw new Exception("Delivery was not Updated");
        }

        public DeliveredDS.deliveredDataTable DeliveredGetMoreThanQuantity(int delivered_quantity)
        {
            if (delivered_quantity < 0)
                throw new Exception ("Quantity should be greater than 0");
            return Adapter.DeliveredGetMoreThanQuantity(delivered_quantity);
        }

        public DeliveredDS.deliveredDataTable DeliveredGetByNum(Guid delivered_num)
        {
            if (delivered_num ==null)
                throw new Exception("Delivered Num is missing ");
            return Adapter.DeliveredGetByNum(delivered_num);
        }

        public DeliveredDS.deliveredDataTable DeliveredSearch(string delivered_component, string delivered_user, string delivered_customer, DateTime delivered_date_from, DateTime delivered_date_to)
        {
            return Adapter.DeliveredSearch(delivered_component, delivered_user, delivered_customer, delivered_date_from, delivered_date_to);
            
        }

        public DeliveredDS.deliveredDataTable DeliveredGetByComponent (Guid delieverd_componenet)
        {
            if (delieverd_componenet==null)
                throw new Exception ("Component Number is missing");
            return Adapter.DeliveredGetByComponent(delieverd_componenet);
        }

        public DeliveredDS.deliveredDataTable DeliveredGetByCustomer (Guid delivered_cusrtomer)
        {
            if (delivered_cusrtomer==null)
                throw new Exception ("Customer Number is missing");
            return Adapter.DeliveredGetByCustomer (delivered_cusrtomer);
        }

        public DeliveredDS.deliveredDataTable DeliveredGetByUser(Guid delivered_user)
        {
            if (delivered_user == null)
                throw new Exception("User Number is missing");
            return Adapter.DeliveredGetByUser(delivered_user);
        }

        public DeliveredDS.deliveredDataTable DeliveredGet()
        {
            
            return Adapter.GetData();
        }

    }
}
